//
//  KBTabbarController.m
//  KBTabbarController
//
//  Created by kangbing on 16/5/31.
//  Copyright © 2016年 kangbing. All rights reserved.
//

#import "KBTabbarController.h"
//#import "OneViewController.h"
//#import "TwoViewController.h"
//#import "ThreeViewController.h"
//#import "FourViewController.h"
#import "KBTabbar.h"

#import "DQRSXViewController.h"
#import <NTalkerGuestIMKit/NTalkerGuestIMKit.h>
#import <NTalkerIMCore/NtalkerDBManager.h>
@interface KBTabbarController ()<UITabBarControllerDelegate,NtalkerChatDelegate>

@end

@implementation KBTabbarController

- (void)viewDidLoad {
    [super viewDidLoad];
//    [[UITabBar appearance] setBackgroundImage:[self imageWithColor:[UIColor whiteColor]]];
    //  设置tabbar
//    [[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
    // 设置自定义的tabbar
    [self setCustomtabbar];
    self.delegate = self;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(unReadMessage:) name:NtalkerUnReadMessageNotification object:nil];

}

-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    BOOL result = [self.viewControllers indexOfObject:viewController]!=2;
    if (result==NO) {
        NtalkerChatParams *chatParems = [[NtalkerChatParams alloc] init];
        //    chatParems.delegate = self;
        chatParems.settingId = @"kf_20117_template_9999";
        chatParems.delegate = self;
        UIViewController *chatVC = [Ntalker ntalker_chatViewControllerWithChatParam:chatParems];
        chatVC.hidesBottomBarWhenPushed=YES;
        [Ntalker ntalker_historyMessageLoadingPattern:1];
        [((UINavigationController*)self.selectedViewController) pushViewController:chatVC animated:YES];
        
    }
    
    return result;
}

- (void)ntalker_navigationBar:(UIView *)navigationBar
{
    navigationBar.backgroundColor = [UIColor colorWithRed:28/255.0 green:32/255.0 blue:41/255.0 alpha:1.0];//修改导航栏背景色
//    for (UIView *subView in navigationBar.subviews) {
//        if ([subView isKindOfClass:[UIImageView class]]) {
//            [subView removeFromSuperview];
//        }else if ([subView isKindOfClass:[UIButton class]]){
//            if (subView.frame.origin.x!=0) {
//                [subView removeFromSuperview];
//            }
//        }
//    }
//    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 40.0f, [UIScreen mainScreen].bounds.size.width, 44.0f)];
//    titleLabel.text = @"客服";
//    titleLabel.textColor = [UIColor whiteColor];
//    titleLabel.textAlignment = NSTextAlignmentCenter;
//    [navigationBar addSubview:titleLabel];
}
-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

- (void)unReadMessage:(NSNotification *)not
{
    NSLog(@"有新的未读消息== \n %@",not.userInfo);
    [self onUnReadMessageWithDict:not.userInfo];
}
///未读消息处理
- (void)onUnReadMessageWithDict:(NSDictionary *)dict
{
//    NSString *settingId = dict[@"settingId"];
    NSNumber *unreadCount = dict[@"unreadCount"];
    
    dispatch_async(dispatch_get_main_queue(), ^{
        if (unreadCount.integerValue==0) {
            self.viewControllers[2].tabBarItem.badgeValue = nil;
        }else
            self.viewControllers[2].tabBarItem.badgeValue = [NSString stringWithFormat:@"%@",unreadCount];
        
    });
    
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
}

- (void)setCustomtabbar{

    KBTabbar *tabbar = [[KBTabbar alloc]init];
//
    [self setValue:tabbar forKeyPath:@"tabBar"];
    NSLog(@"setCustomtabBar");
    [tabbar.centerBtn addTarget:self action:@selector(centerBtnClick:) forControlEvents:UIControlEventTouchUpInside];


}

//-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
//    
//}

- (void)centerBtnClick:(UIButton *)btn{
    DQRSXViewController *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"DQRSXViewController"];
    [((UINavigationController*)self.selectedViewController) pushViewController:vc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




//- (UIImage *)imageWithColor:(UIColor *)color{
//    // 一个像素
//    CGRect rect = CGRectMake(0, 0, 1, 1);
//    // 开启上下文
//    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
//    [color setFill];
//    UIRectFill(rect);
//    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
//    UIGraphicsEndImageContext();
//    
//    return image;
//}


@end
